package com.usian.article.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.article.mapper.AuthorMapper;
import com.usian.article.service.AuthorService;
import com.usian.model.article.pojos.ApAuthor;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author baimugudu
 * @email 2415621370@qq.com
 * @date 2023/8/25 8:41
 */
@Service
@Slf4j
@Transactional
public class AuthorServiceImpl extends ServiceImpl<AuthorMapper,ApAuthor> implements AuthorService {
    @Override
    public ApAuthor findByUserid(Integer id) {
        if(null==id){
            return null;
        }

        QueryWrapper<ApAuthor> apAuthorQueryWrapper = new QueryWrapper<>();
        apAuthorQueryWrapper.lambda().eq(ApAuthor::getUserId,id);

        ApAuthor apAuthor = this.baseMapper.selectOne(apAuthorQueryWrapper);

        return apAuthor;
    }

    @Override
    public ResponseResult saveApAuthor(ApAuthor apAuthor) {
        log.info("apAuthor:{}", JSON.toJSONString(apAuthor));
        apAuthor.setCreatedTime(new Date());
        this.save(apAuthor);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
